﻿using Huioo.Learning.Logging.ConsoleAppWithSerilog;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Json;

namespace ConsoleApp1
{
    internal class Program
    {        
        static void Main(string[] args)
        {
            try
            {
                // 示例1：直接创建日志组件
                using (var userLogger = new LoggerComponent("UserManagement"))
                {
                    userLogger.Information("用户日志组件初始化完成");
                    userLogger.Information("用户 '{Username}' 登录系统", "admin");
                    userLogger.Debug("验证用户权限: {Permission}", "admin");

                    try
                    {
                        int zero = 0;
                        // 模拟一个错误
                        int result = 10 / zero;
                    }
                    catch (Exception ex)
                    {
                        userLogger.Error(ex, "用户操作发生错误");
                    }
                }

                // 示例2：通过日志管理器获取日志组件
                var orderLogger = LoggerManager.GetLogger("OrderProcessing", minimumLevel: LogEventLevel.Debug);
                orderLogger.Information("订单处理日志开始记录");
                orderLogger.Warning("订单 '{OrderId}' 即将超时", "ORD-12345");
                orderLogger.Debug("订单详情: {OrderDetails}", "产品A x 2, 产品B x 1");

                // 模拟其他操作...
                System.Threading.Thread.Sleep(1000);

                // 释放日志组件
                LoggerManager.ReleaseLogger("OrderProcessing");

                Console.WriteLine("日志记录完成，按任意键退出...");
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误: {ex.Message}");
            }
            finally
            {
                // 确保所有日志都被释放
                LoggerManager.ReleaseAllLoggers();
            }
        }
    }

}
